#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int N=2e5+10;
vector<vector<int> > tr(N+5);
vector<int> d(N+5);
int dfs(int u, int v){
        int f = 1;
        for (auto i : tr[u]) {
            if (i == v) continue;
            f = 0;
            d[u] += dfs(i, u);
        }
        if (f) d[u] = 1;
        return d[u];
    };
void solve()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++) tr[i].clear(),d[i]=0;
	for(int i=0;i<n-1;i++)
	{
		int a,b;
		cin>>a>>b;
		tr[a].push_back(b);
		tr[b].push_back(a); 
	}
	int q;
	cin>>q;
	dfs(1,-1);
	while(q--)
	{
		int a,b;
		cin>>a>>b;
		cout<<(ll)d[a]*d[b]<<endl;
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie();
	cout.tie();
	int t;
	cin>>t;
	while(t--)
	{
		solve();
	}

	return 0;
}

